using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.IO;//using FileInfo 
using System.Net;
using System.Text.RegularExpressions;

using System.IO;//using FileInfo class
//using System.Drawing;// do not using the namespace for  Image class  may belongs to  the namespace System.Web.UI.WebControls
namespace AddWaterMark
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string str = Server.MapPath("ImgUpload");
            //Label2.Text = str;
        }

        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (FileUploadPics.HasFile)
            {
                //string FileType = FileUploadPics.PostedFile.ContentType;
                FileInfo file = new FileInfo(FileUploadPics.PostedFile.FileName);
                //if (FileType == "Image/bmp" || FileType == "Image/jpeg" || FileType == "Image/gif")
                if(file.Extension==".bmp"||file.Extension==".jpg"||file.Extension==".gif")
                {
                    //string name = FileUploadPics.PostedFile.FileName;
                    //FileInfo file = new FileInfo(name);//the new name of the new file
                    string FileName = file.Name;//get the name;
                    string WebFilePath = Server.MapPath( FileName);//get the filepath on the server
                    string FileNameWM = "TextMarked_" + file.Name;//get the name of the picture water-marked
                    string WebFilePathWM = Server.MapPath( FileNameWM);//get the filepath of the picture of water-marked on the server
                    if (!File.Exists(WebFilePath))//see whether the WebFilePath exsits
                    {//3
                        try
                        {
                            FileUploadPics.SaveAs(WebFilePath);
                            AddWater(WebFilePath, WebFilePathWM);
                            LblResult.Text = "Congratulation to you ,succeed!";
                            Image2.ImageUrl = WebFilePathWM;
                        }
                        catch (Exception ex)
                        {
                            LblResult.Text = "Failed ,the reason is " + ex.Message;
                        }
                    }
                    else
                    {
                        LblResult.Text = "exists!";
                    }
                }
                else
                {
                    LblResult.Text = "Wrong type";
                }
            }
            else
            {
                LblResult.Text = "Please select a picture";
            }
        }

     
                        

        protected void AddWater(string Path, string PathText)
        {
            string TextToAdd = TxbToAdd.Text;//read the information
            System.Drawing.Image Image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics Graphics = System.Drawing.Graphics.FromImage(Image);
            Graphics.DrawImage(Image, 0, 0, Image.Width, Image.Height);
            System.Drawing.Font Font = new System.Drawing.Font("Verdana", 60);
            System.Drawing.Brush Brush = new System.Drawing.SolidBrush(System.Drawing.Color.DeepSkyBlue);
            Graphics.DrawString(TextToAdd, Font, Brush, 35, 35);
            Graphics.Dispose();
            Image.Save(PathText);
            Image.Dispose();
        }


    }
}
